<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="社团ID" prop="clubId">
      <el-input v-model="dataForm.clubId" placeholder="社团ID"></el-input>
    </el-form-item>
    <el-form-item label="社团名称" prop="clubName">
      <el-input v-model="dataForm.clubName" placeholder="社团名称"></el-input>
    </el-form-item>
    <el-form-item label="活动名称" prop="activityName">
      <el-input v-model="dataForm.activityName" placeholder="活动名称"></el-input>
    </el-form-item>
    <el-form-item label="活动时间" prop="activityTime">
      <el-input v-model="dataForm.activityTime" placeholder="活动时间"></el-input>
    </el-form-item>
    <el-form-item label="活动人数" prop="activityNum">
      <el-input v-model="dataForm.activityNum" placeholder="活动人数"></el-input>
    </el-form-item>
    <el-form-item label="申请时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="申请时间"></el-input>
    </el-form-item>
    <el-form-item label="申请资料" prop="url">
      <el-input v-model="dataForm.url" placeholder="申请资料"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          clubId: '',
          clubName: '',
          activityName: '',
          activityTime: '',
          activityNum: '',
          createTime: '',
          url: ''
        },
        dataRule: {
          clubId: [
            { required: true, message: '社团ID不能为空', trigger: 'blur' }
          ],
          clubName: [
            { required: true, message: '社团名称不能为空', trigger: 'blur' }
          ],
          activityName: [
            { required: true, message: '活动名称不能为空', trigger: 'blur' }
          ],
          activityTime: [
            { required: true, message: '活动时间不能为空', trigger: 'blur' }
          ],
          activityNum: [
            { required: true, message: '活动人数不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '申请时间不能为空', trigger: 'blur' }
          ],
          url: [
            { required: true, message: '申请资料不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/club/clubactivity/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.clubId = data.clubActivity.clubId
                this.dataForm.clubName = data.clubActivity.clubName
                this.dataForm.activityName = data.clubActivity.activityName
                this.dataForm.activityTime = data.clubActivity.activityTime
                this.dataForm.activityNum = data.clubActivity.activityNum
                this.dataForm.createTime = data.clubActivity.createTime
                this.dataForm.url = data.clubActivity.url
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/club/clubactivity/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'clubId': this.dataForm.clubId,
                'clubName': this.dataForm.clubName,
                'activityName': this.dataForm.activityName,
                'activityTime': this.dataForm.activityTime,
                'activityNum': this.dataForm.activityNum,
                'createTime': this.dataForm.createTime,
                'url': this.dataForm.url
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
